###########################################
#  Primary Divisions: How Voters Evaluate Policy and Group Differences in Intra-Party Contests
#   - Forthcoming at The Journal of Politics
#   - Henderson et al 2021
#
###########################################
#  - code by S. Goggin & J. Henderson
########################################################
# This file produces scores that measure 'correct' inferences by respondents on the libcon placement experimental arm
########################################################
# inputs :: /data/data_matrix_scored.Rdata;
#				 :: /data/candidate_matrix.csv


# outputs ::
#         :: correct_placements.Rdata

#dirs="~/Dropbox/replication0/"
#dirs should be set here or in runR.R

rm(list=ls()[which(ls()!='dirs')])

library(ggplot2)
library(stringr)

# messy function to reorder by some estimate value
lableOrder=function(xmat,labels,label.groups,omits,o.column){

	# denote which label is to be omitted on the label
	for(i in 1:length(omits)){
		labels[which(labels==omits[i])]=paste('omit',labels[which(labels==omits[i])],sep='_')
	}

	# break groups into levels
	un_group=unique(label.groups)

	# this is the item to sort on, typically global or independent
	xm=xmat[,o.column]

	# vector which will contain row order
	xo=1:length(xm)

	# rearranging roworder within level
	for(j in 1:length(un_group)){
		ix=which(label.groups==un_group[j])
		if(length(ix)>2){
			ix=ix[!grepl(labels[ix],pattern='omit')]
			xo[ix]=xo[ix][order(xm[ix])]
		}
	}
	return(xmat[xo,])
}

reOrder=function(x,o){
	ix=array(NA,nrow(x))
	for(i in 1:length(o)){
		ix[i]=which(x$iv_order==o[i])
	}
	return(x[ix,])
}

setwd(dirs)
load("data/vdl.cces_stacked_unmatched.Rdata")

###########################################
###First, need to stack based on candidates, not just candidate pairs (and also get text out for labels later)

#This has leaners as independents, which is incorrect
#cces_stacked$pid3clean <- ifelse(cces_stacked$pid3=="Democrat",-1,ifelse(cces_stacked$pid3=="Republican",1,0))

library(car)

load('data/vdl.data_matrix_scored.Rdata')
candidate_matrix=data_matrix
#read.csv("csv/candidate_matrix_scored.csv",header=T,stringsAsFactors=F)[,-c(1)]

###########################################
###Then, produce models w/ Clustered SEs

#Function from: http://scholar.byu.edu/jgubler/book/clustered-standard-errors-r
#Need this for clustered standard errors
clse.f <- function(dat,fm, cluster){
 require(sandwich)
 require(lmtest)
 not <- attr(fm$model,"na.action")
if( ! is.null(not)){
  cluster <- cluster[-not]
    dat <- dat[-not,]
}
 with(dat,{
 M <- length(unique(cluster))
 N <- length(cluster)
 K <- fm$rank
 dfc <- (M/(M-1))*((N-1)/(N-K))
 uj <- apply(estfun(fm),2, function(x) tapply(x, cluster, sum));
 vcovCL <- dfc*sandwich(fm, meat=crossprod(uj)/N)
 coeftest(fm, vcovCL)
 }
 )
}

############################################################

############################################################
############################################################


ideo_extreme <- subset(candidate_matrix,(candidate_matrix$conjoints==1|candidate_matrix$conjoints==2|candidate_matrix$conjoints==5|candidate_matrix$conjoints==6))

#Standardizing the direction across 1,2,5,6 JAHconjoints w/ Lib = 0, Cons = 1
ideo_extreme$dv_libcon <- ifelse(
ideo_extreme$conjoints==1& ideo_extreme$dv_choice==1,0,ifelse(
ideo_extreme$conjoints==1& ideo_extreme$dv_choice==0,1,ifelse(
ideo_extreme$conjoints==5& ideo_extreme$dv_choice==1,0,ifelse(
ideo_extreme$conjoints==5& ideo_extreme$dv_choice==0,1,ifelse(
ideo_extreme$conjoints==2& ideo_extreme$dv_choice==1,1,ifelse(
ideo_extreme$conjoints==2& ideo_extreme$dv_choice==0,0,ifelse(
ideo_extreme$conjoints==6& ideo_extreme$dv_choice==1,1,ifelse(
ideo_extreme$conjoints==6& ideo_extreme$dv_choice==0,0,NA))))))))

ideo_extreme$pty <- ifelse(ideo_extreme$conjoints==1 | ideo_extreme$conjoints==5,0,1)

resp_id=ideo_extreme$respondent[which(ideo_extreme$candidate=='A')]

libcon=ideo_extreme$dv_libcon[which(ideo_extreme$candidate=='A')]-ideo_extreme$dv_libcon[which(ideo_extreme$candidate=='B')]
mudif=ideo_extreme$scores[which(ideo_extreme$candidate=='A')]-ideo_extreme$scores[which(ideo_extreme$candidate=='B')]

ideo_extreme$correct=correct=as.numeric(libcon>0 & mudif>0 | libcon<0 & mudif<0)
#incorrect=as.numeric(libcon>0 & mudif<0 | libcon<0 & mudif>0 | libcon==0)
ix_correct=tapply(correct,resp_id,sum)
save(ix_correct,file='data/correct_placements.Rdata')

#END
